<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="author" content="Yb">
    <title>模拟select事件</title>
    <script src="../js/jquery-3.1.1.js"></script>
    <style>
        *{
            box-sizing: border-box;
        }
        ul,li{
            margin: 0;
            padding: 0;
            list-style: none;
        }
        #box{
            width: 200px;
            height: 30px;
            font-family: 'Microsoft YaHei';
            margin: 100px auto;
            line-height: 30px;
            background-color: red;
            position: relative;
            text-align: center;
        }
        #box a{
            float: left;
            width: 100px;
            text-decoration: none;
            cursor: default;
            color: black;
        }
        #box span{
            background-color: lightgray;
            float: left;
            width: 100px;
            cursor: pointer;
        }
        #box ul{
            width: 100px;
            border: 1px solid #ccc;
            display: none;
            position: absolute;
            top: 29px;
            left: 100px;
        }
        #box ul li{
            border-bottom: 1px dashed #ccc;
        }
        #box ul li:last-child{
            border-bottom: none;
        }
        #box ul li:hover{
            background-color: #ccc;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div id="box">
        <div>
            <a href="javascript:;">项目选择</a>
            <span>未选择</span>
        </div>
        <ul class="list">
            <li>HTML</li>
            <li>CSS</li>
            <li>JavaScript</li>
            <li>jQuery</li>
        </ul>
    </div>
</body>
</html>

<script>
    $('#box').click(function(ev){
        // 阻止事件的传播
        ev.stopPropagation();
        // console.log('1');
        $('#box ul').toggle();

        $(this).find('ul li').each(function(){
            $(this).click(function(){
                $('#box span').html($(this).html());
            })
        })
    })

    // 点击空白处隐藏列表
    $(document).click(function(){
        // console.log('111');
        $('#box ul').hide();
    })
</script>